<?php


namespace App\Transforms;


use App\Enum\CarEnum;
use App\Enum\UserEnum;
use App\Models\Orders;
use App\Models\UploadFile;
use App\Models\UserCoupon;
use App\Repositories\UploadFileRepository;
use BobCoder\Construct\Foundation\Transform\TransformContract;
use Illuminate\Support\Facades\DB;

class UserTransformer implements TransformContract
{

    public function transform($attributes)
    {
        if (!$attributes) return $attributes;

        if ($attributes->identity_photo) {
            $attributes->identity_photo = (new UploadFileRepository())->getFilePathList($attributes->identity_photo);
        }
        unset($attributes->password);

        return $attributes;
    }


    /**
     * 格式化数据
     * @param $list
     * @return mixed
     * @Author 姿势就是力量
     */
    public function transformList($list)
    {
        if (!$list) return $list;

        foreach ($list as &$item) {

            $item['sex'] = UserEnum::sexMessage($item['sex']);

            // TODO 统计订单数
            $item['order'] =Orders::query()->where('user_id',$item['id'])->whereIn('status',['4','5','6'])->count();
            $item['count'] =UserCoupon::query()->where('user_id',$item['id'])->where('overdue_time','>',time())->where('status',1)->count(); //优惠券统计
        }

        return $list;
    }

}
